
/* Copyright Statement:
*
* This software/firmware and related documentation ("MediaTek Software") are
* protected under relevant copyright laws. The information contained herein is
* confidential and proprietary to MediaTek Inc. and/or its licensors. Without
* the prior written permission of MediaTek inc. and/or its licensors, any
* reproduction, modification, use or disclosure of MediaTek Software, and
* information contained herein, in whole or in part, shall be strictly
* prohibited.
*
* MediaTek Inc. (C) 2014. All rights reserved.
*
* BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
* ON AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL
* WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NONINFRINGEMENT. NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH
* RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
* INCORPORATED IN, OR SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES
* TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
* RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
* OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK
* SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE
* RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
* STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S
* ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE
* RELEASED HEREUNDER WILL BE, AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE
* MEDIATEK SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
* CHARGE PAID BY RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* The following software/firmware and/or related documentation ("MediaTek
* Software") have been modified by MediaTek Inc. All revisions are subject to
* any receiver's applicable license agreements with MediaTek Inc.
*/
package com.android.camera.manager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.SeekBar;
import android.widget.TextView;

import com.android.camera.CameraActivity;
import com.android.camera.FeatureSwitcher;
import com.android.camera.Log;
import com.android.camera.ModeChecker;
import com.android.camera.R;
import com.android.camera.SettingUtils;
import com.android.camera.bridge.ParametersExt;
import com.android.camera.ui.ModePickerScrollView;


import com.mediatek.camera.ISettingCtrl;
import com.mediatek.camera.platform.Parameters;
import com.mediatek.camera.setting.ParametersHelper;
import com.mediatek.camera.setting.SettingConstants;
import com.mediatek.camera.setting.preference.IconListPreference;
import com.mediatek.camera.setting.preference.ListPreference;


import com.android.camera.ui.RGK_RotateImageView;
import com.android.camera.ui.RotateImageView;

import android.view.Gravity;

public class RGKSubSettingPicker extends ViewManager implements View.OnClickListener,
       View.OnLongClickListener {
   private static final String TAG = "ModePicker";

   private ListPreference mModePreference;
   //add by xiaodong start
   private ParametersExt mParametersExt;
   
   
   protected ISettingCtrl mISettingCtrl;
   private ListPreference mEffectPreference;
   CameraActivity mContext;
   
   private String mSettingKey;
   
   private int mEntryCount = 0;
   
   public interface OnModeChangedListener {
       void onModeChanged(int newMode);
   }

   // can not change this sequence
   // Before MODE_VIDEO is "capture mode" for UI,switch "capture mode"
   // remaining view should not show
   
   public static final int EFFECT_NONE = 0;
   public static final int EFFECT_MONO = 1;
   public static final int EFFECT_SEPIA = 2;
   public static final int EFFECT_NEGATIVE = 3;
   public static final int EFFECT_AQUA = 4;
   public static final int EFFECT_BLACKBORAD = 5;
   public static final int EFFECT_WHITEBOARD = 6;
   

   public static final int MODE_PHOTO = 0;
   public static final int MODE_HDR = 1;
   public static final int MODE_FACE_BEAUTY = 2;
   public static final int MODE_PANORAMA = 3;
   public static final int MODE_ASD = 4;
   public static final int MODE_PHOTO_PIP = 5;
   public static final int MODE_STEREO_CAMERA = 6;
   public static final int MODE_VIDEO = 7;
   public static final int MODE_VIDEO_PIP = 8;


   public static final int MODE_NUM_ALL = 9;
   public static final int OFFSET = 100;
   private static final int OFFSET_STEREO_PREVIEW = OFFSET;
   private static final int OFFSET_STEREO_SINGLE = OFFSET * 2;

   public static final int MODE_PHOTO_3D = OFFSET_STEREO_PREVIEW + MODE_PHOTO;
   public static final int MODE_VIDEO_3D = OFFSET_STEREO_PREVIEW + MODE_VIDEO;

   public static final int MODE_PHOTO_SGINLE_3D = OFFSET_STEREO_SINGLE + MODE_PHOTO;
   public static final int MODE_PANORAMA_SINGLE_3D = OFFSET_STEREO_SINGLE + MODE_PANORAMA;

   private static final int DELAY_MSG_HIDE_MS = 3000; // 3s
   private static final int MODE_DEFAULT_MARGINBOTTOM = 100;
   private static final int MODE_DEFAULT_PADDING = 20;
   private static final int MODE_MIN_COUNTS = 4;
   private LinearLayout.LayoutParams mLayoutParams = new LinearLayout.LayoutParams(
           LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

   private static final int[] MODE_ICONS_HIGHTLIGHT = new int[MODE_NUM_ALL];
   private static final int[] MODE_ICON_ORDER = {
       MODE_PHOTO, MODE_STEREO_CAMERA, MODE_PHOTO_PIP,
       MODE_FACE_BEAUTY, MODE_PANORAMA};
   static {
       MODE_ICONS_HIGHTLIGHT[MODE_PHOTO] = R.drawable.ic_mode_photo_focus;
       MODE_ICONS_HIGHTLIGHT[MODE_FACE_BEAUTY] = R.drawable.ic_mode_facebeauty_focus;
       MODE_ICONS_HIGHTLIGHT[MODE_PANORAMA] = R.drawable.ic_mode_panorama_focus;
       MODE_ICONS_HIGHTLIGHT[MODE_PHOTO_PIP] = R.drawable.ic_mode_pip_focus;
       MODE_ICONS_HIGHTLIGHT[MODE_STEREO_CAMERA] = R.drawable.ic_mode_refocus_focus;
   };
   private static final int[] MODE_ICONS_NORMAL = new int[MODE_NUM_ALL];
   static {
       MODE_ICONS_NORMAL[MODE_PHOTO] = R.drawable.ic_mode_photo_normal;
       MODE_ICONS_NORMAL[MODE_FACE_BEAUTY] = R.drawable.ic_mode_facebeauty_normal;
       MODE_ICONS_NORMAL[MODE_PANORAMA] = R.drawable.ic_mode_panorama_normal;
       MODE_ICONS_NORMAL[MODE_PHOTO_PIP] = R.drawable.ic_mode_pip_normal;
       MODE_ICONS_NORMAL[MODE_STEREO_CAMERA] = R.drawable.ic_mode_refocus_normal;
   };

   private final RGK_RotateImageView[] mModeViews = new RGK_RotateImageView[20];
   private ModePickerScrollView mScrollView;
   private int mCurrentMode = -1;
   private OnModeChangedListener mModeChangeListener;
   private OnScreenToast mModeToast;
   private int mDisplayWidth;
   private int mModeWidth;
   private int mModeMarginBottom = MODE_DEFAULT_MARGINBOTTOM;
   //add by xiaodong.cheng
   private View faceBeautyView;
   
   private LinearLayout mViewGroupColorEffects;
   
   
   public RGKSubSettingPicker(CameraActivity context, String key) {
       super(context, VIEW_LAYER_OVERLAY);
       
       mContext = context;
       
       mSettingKey = key;
   }


   private void highlightCurrentMode() {
	   int index = mEffectPreference.findIndexOfValue(mEffectPreference.getValue());
	   
       //int index =mEffectPreference.findIndexOfValue(mEffectPreference.getEntry()) ;
       IconListPreference iconPref = ((IconListPreference) mEffectPreference);
       
       Log.i(TAG, "current effect value i" + index +","
       		+ "mEffectPreference.getValue():"+mEffectPreference.getValue() 
       		+" ,mEffectPreference.getEntry():"+mEffectPreference.getEntry());
       //index = 0;
       
       for (int i = 0; i < MODE_NUM_ALL; i++) {
           if (mModeViews[i] != null) {
               if (i == index) {
                   mModeViews[i].setImageResource(iconPref.getIconIdRgkHi(i));

               } else {
            	   mModeViews[i].setImageResource(iconPref.getIconIdRgk(i));
               }
           }
           
       }
   }

   

   @Override
   protected View getView() {
       clearListener();
       View view = inflate(R.layout.rgk_sub_setting_layout);
       
       mScrollView = (ModePickerScrollView) view.findViewById(R.id.mode_picker_scroller);
       mViewGroupColorEffects = (LinearLayout) view.findViewById(R.id.mode_picker);
       
       mISettingCtrl =  mContext.getISettingCtrl();
       mEffectPreference = mISettingCtrl.getListPreference(mSettingKey);
       
       //mEffectPreference.reloadValue();
       
       if (mEffectPreference.getEntries() != null)
       {
	       for (int i = 0, len = mEffectPreference.getEntries().length; i < len; i++) {
	           Log.d(TAG, "mEffectPreference getEntries["+i+"]" + mEffectPreference.getEntries()[i].toString() );
	           
	           mModeViews[i] = new RGK_RotateImageView(getContext()) ;
	           mModeViews[i].setDrawText(mEffectPreference.getEntries()[i].toString());
	          
	           /*
	           IconListPreference iconPref = ((IconListPreference) mEffectPreference);
	           mModeViews[i].setImageResource(iconPref.getIconIdLarge(i));*/
	           
	           
	           mViewGroupColorEffects.addView(mModeViews[i]);
	           
	           mEntryCount++;
	       }
       }
      
   
    

       DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
       mDisplayWidth = Math.min(metrics.widthPixels, metrics.heightPixels);
       mModeWidth = getModeWidth();
       mModeMarginBottom = getDefaultMarginBottom();
       applyListener();
       highlightCurrentMode();
       return view;
   }

   private void applyListener() {
       for (int i = 0; i < MODE_NUM_ALL; i++) {
           if (mModeViews[i] != null) {
               mModeViews[i].setOnClickListener(this);
               mModeViews[i].setOnLongClickListener(this);
           }
       }
   }

   private void clearListener() {
       for (int i = 0; i < MODE_NUM_ALL; i++) {
           if (mModeViews[i] != null) {
               mModeViews[i].setOnClickListener(null);
               mModeViews[i].setOnLongClickListener(null);
               mModeViews[i] = null;
           }
       }
   }

   @Override
   public void onClick(View view) {
       Log.d(TAG, "@@##onClick(" + view + ")");
     
       
       for (int i = 0; i < MODE_NUM_ALL; i++) {
           if (mModeViews[i] == view) {
        	   
        	   Log.d(TAG, "@@##onClick view[" + i + "]" +" clicked");
        	   
        	   mContext.onSettingChanged(mSettingKey, mEffectPreference.getEntryValues()[i].toString());
        	   //mEffectPreference.setValue(mEffectPreference.getEntryValues()[i].toString());
        	   
        	    mEffectPreference.setValue(mEffectPreference.getEntryValues()[i].toString());
        	   
        	    mContext.getCameraDeviceCtrl().applyParameters(false);
        	   
        	   getContext().notifyPreferenceChanged(mEffectPreference);
        	   
        	   highlightCurrentMode();
        	   
        	   // for RGK setting new page 
        	  // getContext().getRGKSettingPicker().onRefresh();
        	   getContext().getRGKSettingManagerNewPage().onRefresh();
           }
       }
   }


   public void onRefresh() {
       Log.d(TAG, "onRefresh() mCurrentMode=" + mCurrentMode);
       // get counts of mode supported by back camera and compute the margin
       // bottom between mode icon.
       int supportModes = ModeChecker.modesShowInPicker(getContext(), 0);
       if (supportModes < MODE_MIN_COUNTS && supportModes > 1) {
           mModeMarginBottom = (mDisplayWidth - supportModes * mModeWidth) / (supportModes - 1);
       }
       Log.d(TAG, "mModeMarginBottom:" + mModeMarginBottom);
       mLayoutParams.setMargins(0, 0, 0, 0);
       
       Log.d(TAG, "onRefresh() mEntryCount=" + mEntryCount);
       {
    	   int leftSize = mDisplayWidth - mEntryCount *mModeViews[0].getWidth();
    	   int marginSize =  leftSize / (mEntryCount * 2);
    	   /*
    	   if (mEntryCount == 2)
    		   mLayoutParams.setMargins(70, 0, 70, 0);
    	   else if (mEntryCount == 3)
    		   mLayoutParams.setMargins(40, 0, 40, 0); 
    	   
    	   else if (mEntryCount == 4)
    		   mLayoutParams.setMargins(20, 0, 20, 0); 
    	   
    	   else if (mEntryCount == 5)
    		   mLayoutParams.setMargins(10, 0, 10, 0); 
    	   else
    		   mLayoutParams.setMargins(0, 0, 0, 0);*/
       }

       int visibleCount = 0;
       for (int j = 0; j < MODE_NUM_ALL; j++) {
           if (mModeViews[j] != null) {
               boolean visible = ModeChecker.getModePickerVisible(getContext(), getContext()
                       .getCameraId(), j);
               // check vFB
               // if vFB support, FB not need show in the mode picker line
               if (MODE_FACE_BEAUTY == j && FeatureSwitcher.isVfbEnable()) {
                   visible = false;
               }
               
               visible = true;
               // mLayoutParams.setLayoutDirection(Gravity.CENTER);
               // mLayoutParams.gravity = Gravity.END;
               mModeViews[j].setVisibility(visible ? View.VISIBLE : View.GONE);
               mModeViews[j].setLayoutParams(mLayoutParams);
              // mModeViews[j].setPadding(MODE_DEFAULT_PADDING, MODE_DEFAULT_PADDING,  0, 0);
               
               {
            	   int leftSize = mDisplayWidth - mEntryCount *mModeViews[0].getWidth();
            	   int marginSize =  leftSize / (mEntryCount * 2);
            	   
            	   if (mEntryCount == 2)
            		   mModeViews[j].setPadding(0, 100,  0, 100);
            	   else if (mEntryCount == 3)
            		   mModeViews[j].setPadding(0, 60,  0, 60);
            	   
            	   else if (mEntryCount == 4)
            		   mModeViews[j].setPadding(0, 40,  0, 40);
            	   
            	   else if (mEntryCount == 5)
            		   mModeViews[j].setPadding(0, 20,  0, 20);
            	   else
            		   mModeViews[j].setPadding(0, 10,  0, 10);
               }
               
              // mModeViews[j].setHorizontalGravity(Gravity.CENTER_HORIZONTAL);
               if (visible) {
                   visibleCount++;
               }
               
               mEffectPreference = mISettingCtrl.getListPreference(mSettingKey);
  
           }
       }
       // set margin botton of the last mode icon as 0.
       /*
       for (int i = MODE_ICON_ORDER.length - 1; i >= 0; i--) {
           int index = MODE_ICON_ORDER[i];
           if (mModeViews[index] != null && mModeViews[index].getVisibility() == View.VISIBLE) {
               LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                       LinearLayout.LayoutParams.WRAP_CONTENT,
                       LinearLayout.LayoutParams.WRAP_CONTENT);
               params.setMargins(0, 0, 0, 0);
               mModeViews[index].setLayoutParams(params);
               break;
           }
       }
		*/
      // if (visibleCount <= 1) { // to enable/disable background
       if (false) { // to enable/disable background
           mScrollView.setVisibility(View.GONE);
       } else {
           mScrollView.setVisibility(View.VISIBLE);
       }
       highlightCurrentMode();
   }

   @Override
   public boolean onLongClick(View view) {
       Log.d(TAG, "onLongClick(" + view + ")");
       if (view.getContentDescription() != null) {
           if (mModeToast == null) {
               mModeToast = OnScreenToast.makeText(getContext(), view.getContentDescription());
           } else {
               mModeToast.setText(view.getContentDescription());
           }
           mModeToast.showToast();
       }
       // don't consume long click event
       return false;
   }

   @Override
   public void setEnabled(boolean enabled) {
       super.setEnabled(enabled);
       if (mScrollView != null) {
           mScrollView.setEnabled(enabled);
       }
       for (int i = 0; i < MODE_NUM_ALL; i++) {
           if (mModeViews[i] != null) {
               mModeViews[i].setEnabled(enabled);
               mModeViews[i].setClickable(enabled);
           }
       }
   }

   @Override
   protected void onRelease() {
       super.onRelease();
       mModeToast = null;
   }

   private int getModeWidth() {
       Bitmap bitmap = BitmapFactory.decodeResource(getContext().getResources(),
               MODE_ICONS_NORMAL[MODE_PHOTO]);
       int bitmapWidth = bitmap.getWidth();
       return bitmapWidth + MODE_DEFAULT_PADDING * 2;

   }

   private int getDefaultMarginBottom() {
       // default show three and half mode icons
       return (mDisplayWidth - MODE_MIN_COUNTS * mModeWidth) / (MODE_MIN_COUNTS - 1)
               + (mModeWidth / (2 * (MODE_MIN_COUNTS - 1)));
   }

   public void setModePreference(ListPreference pref) {
       mModePreference = pref;
   }
}
